﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace ClassCommon
{
    public class HTMLBuilderHelper
    {
        /// <summary>
        ///  根据datatable凭借列
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string BuilderColsWithDataTable(DataTable dt)
        {
            // 列拼接
            System.Text.StringBuilder colsBuilder = new System.Text.StringBuilder();
            colsBuilder.AppendLine("<tr>");

            for (int c = 0; c < dt.Columns.Count; c++)
            {
                string columnName = dt.Columns[c].ColumnName.ToLower();
                if (columnName.Contains("t") || columnName.Contains("c")) { columnName = ""; }
                colsBuilder.AppendLine(string.Format("<th>{0}</th>", columnName));
            }

            colsBuilder.AppendFormat("</tr>");

            return colsBuilder.ToString();
        }

        /// <summary>
        /// 根据dataTable拼接行
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string BuilderRowsWithDataTable(DataTable dt)
        {
            // 行拼接
            System.Text.StringBuilder rowsBuilder = new System.Text.StringBuilder();
            foreach (DataRow row in dt.Rows)
            {
                rowsBuilder.AppendLine("<tr>");

                for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                {
                    rowsBuilder.AppendLine(string.Format("<td>{0}</td>", row[colIndex].ToString()));
                }

                rowsBuilder.AppendLine("</tr>");
            }

            return rowsBuilder.ToString();
        }
    }
}
